package com.cyq.day19;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

public class MySwing extends JFrame {
    static String userName="aaaaaa";
    static char[] password=new char[]{65,65,65,65,65,65};
    public void initSwing() {
            JFrame frame = new JFrame();
            frame.setTitle("登陆界面");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setBounds(300, 100, 500, 500);
            frame.setLayout(null);
            //用户名框
            JLabel label1 = new JLabel();
            label1.setBounds(50, 50, 100, 100);
            label1.setText("用户名:");
            frame.add(label1);
            //密码框
            JLabel label2 = new JLabel();
            label2.setBounds(50, 100, 100, 100);
            label2.setText("密码:");
            frame.add(label2);
            //用户名输入框
            JTextField textField = new JTextField();
            textField.setBounds(200, 100, 200, 20);
            frame.add(textField);
            //密码输入框
            JPasswordField passwordField = new JPasswordField();
            passwordField.setBounds(200, 150, 200, 20);
            frame.add(passwordField);
            //登录按钮
            JButton button1 = new JButton();
            button1.setBounds(60, 300, 70, 50);
            button1.setText("登录");
            frame.add(button1);
            //清空按钮
            JButton button2 = new JButton();
            button2.setBounds(200, 300, 70, 50);
            button2.setText("清空");
            frame.setVisible(true);
            frame.add(button2);
            //pack();
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (textField.getText().equals(userName) && Arrays.equals(passwordField.getPassword(), password)) {
                        JOptionPane.showMessageDialog(null, "登录成功");
                        com.cyq.day19.qq.MySwing mySwin=new com.cyq.day19.qq.MySwing(textField.getText());
                        mySwin.setVisible(true);
                        frame.dispose();
                    } else {
                        JOptionPane.showMessageDialog(null, "用户名或密码错误");
                    }
                }
            });
            button1.getActionListeners();
            button2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    textField.setText(null);
                    passwordField.setText(null);
                }
            });
            button2.getActionListeners();
        }

}
